package com.bwhx.eruler.controller.casdilly;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.AlertRuler;
import com.bwhx.eruler.bean.casdilly.Linkman;
import com.bwhx.eruler.bean.casdilly.SizeRule;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.AlertRulerService;
import com.bwhx.eruler.util.Constant;

@Controller
@RequestMapping("/alertruler")
public class AlertRulerController extends BaseController {
	
	@Autowired
	public AlertRulerService alertRulerService; 
	
	@ResponseBody
	@RequestMapping("/list.ashx")
	public HashMap<String,Object> list(){
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		try {
			List<AlertRuler> arlist = alertRulerService.list();
			resultMap.put(Constant.DATA, arlist);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}
		
		
		return resultMap;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	 public Map<String,Object> linkmanList(Integer page,Integer rows,AlertRuler ar,HttpSession session) {
		
		Staff staff  = (Staff) session.getAttribute("staff");
		if(null==staff.getUserType()||1!=staff.getUserType()){
			ar.setCreatorID(staff.getsId().toString());

		}
		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,ar);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<AlertRuler> list = alertRulerService.list(p);
			Integer count = alertRulerService.count(ar);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.del.ashx")
	 public Map<String,String> OfflineOrderDel(AlertRuler ar){
		Map<String,String> resultMap = new HashMap<String, String>();
		resultMap.put("status", "1");
		alertRulerService.delete(ar);
	
	    return resultMap;  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdate.ashx")
	public String saveOrUpdateFenchang(AlertRuler ar) {
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			
			if(ar.getaRID().equals("-1")){
				ar.setCreatorID(sessionUser.getsId().toString());
				ar.setCreateTime(new Date());
				alertRulerService.insert(ar);
				
			
				
				
			}else{
				
				ar.setModifierID (sessionUser.getsId().toString());
				ar.setModifyTime(new Date());
				
				alertRulerService.update(ar);

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }

}
